package com.huatai.exchange.impl;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.common.utils.StringUtil;
import com.faneui.common.utils.ZipUtil;
import com.faneui.integration.smb.SmbFileUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.huatai.exchange.dal.model.ExcFileLoadInfo;
import com.huatai.exchange.facade.enums.LoadRecordStatusEnum;
import com.huatai.exchange.service.FileLoadService;
import com.huatai.exchange.utils.FileDealUtils;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class FileLoadServiceImpl implements FileLoadService {

    private static final Logger logger = LoggerFactory.getLogger(FileLoadService.class);

    @Value("${tmp_file_path}")
    private String tmpFilePath;

    @Override
    public void checkFileAvailableMark(ExcFileLoadInfo loadInfo, List<String> smbFileNames, Map<String, Object> resvMap) throws BusinessException {
        if (ListUtils.isEmpty(smbFileNames)) {
            throw new BusinessException("标志文件未到");
        }
        String markName = FileDealUtils.formatStr(loadInfo.getMarkName(), resvMap);
        String[] markNames = markName.split(",");
        for (String name : markNames) {
            if (!smbFileNames.contains(name)) {
                throw new BusinessException("标志文件[" + name + "]未到");
            }
        }
    }

    @Override
    public void downloadFile(ExcFileLoadInfo loadInfo, Map<String, Object> resvMap) throws BusinessException {

    }

    @Override
    public void downloadAndZipFiles(String downloadFileName,String srcName, String smbDirUrl, List<String> smbFileNames, Map<String, Object> resvMap) throws BusinessException {
        File tmpDir = null;
        try {
            String tmpDirPath = tmpFilePath + UUID.randomUUID().toString() + "/";
            tmpDir = new File(tmpDirPath);
            tmpDir.mkdirs();
            List<String> filePathList = Lists.newArrayList();
            NtlmPasswordAuthentication auth = SmbFileUtils.createAuthHdisk();
            for (String smbFileName : smbFileNames) {
                // 文件名匹配
                if (StringUtil.wildcardMatch(smbFileName, srcName)) {
                    logger.info("文件：" + smbFileName);
                    String path = tmpDirPath + smbFileName;
                    File destFile = new File(path);
                    String smbFileUrl = smbDirUrl+ smbFileName ;
                    SmbFileUtils.copyLocalFileFromRemote(destFile, smbFileUrl, auth);
                    filePathList.add(path);
                }
            }
            ZipUtil.zip(downloadFileName, filePathList);
            logger.info("压缩完成");
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        } finally {
            tmpDir.deleteOnExit();
        }
    }
}
